<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
<#include "../common/header.ftl">

    <script type="text/javascript">
        $(function () {

            $('#pagination').twbsPagination({
                        totalPages:${result.pages} || 1,
                    startPage
            :${result.pageNum},
            visiblePages:${result.pageSize},
            first:"首页",
                    prev
            :
            "上页",
                    next
            :
            "下页",
                    last
            :
            "尾页",
                    //event 表示事件对象
                    //page 表示页码: 当前页
                    onPageClick
            :
            function (event, page) {
                //讲当前页设置page值
                $("#currentPage").val(page);
                //表单提交gjk,mb/.
                $("#searchForm").submit();
            }
        })
            ;

            //修改按钮
            $(".editBtn").click(function () {
                $("#editForm")[0].reset();
                var json = $(this).data("json");
                $("#strategyDetailId").val(json.id);
                $("#title").val(json.title);
                $("#sequence").val(json.sequence);
                $("#state").val(json.state);
                $("#coverUrl").val(json.coverUrl);
                $("#coverImg").attr('src',json.coverUrl);
                //回显攻略下拉框
                $("#strategy option[value="+json.strategyId+"]").prop('selected',true);
                //让插件执行更新操作
                $("#strategy").trigger("chosen:updated");

                //回显表单中的攻略分类下拉框
                var html = "<option value="+json.catalogId+" selected>"+json.catalogName+"</option>";
                $("#strategyCatalog").html(html);
                //让插件执行更新操作
                $("#strategyCatalog").trigger("chosen:updated");

                //回显文章内容
                $.get('/strategyDetail/getContentById.do',{id:json.id},function(data){
                    ckeditor.setData(data.content);
                })

                $("#strategyDetailModal").modal('show');
            })

            //保存按钮
            $("#saveBtn").click(function () {
                //把编辑器的内容设置到textarea中
                 var data=ckeditor.getData();
                 $("#editor").val(data);


                $("#editForm").ajaxSubmit(function (data) {
                    showMessager(data);
                });
            })

            //新增按钮
            $(".addBtn").click(function () {
                $("#editForm")[0].reset();
                $("#strategyDetailId").val("");
                $("#coverUrl").val("");
                $("#coverImg").attr('src',"");
                $("#coverImg").attr('width',0);
                $("#strategyDetailModal").modal('show');

            })

            //获取所有攻略
            $.get('/strategy/listAll.do',function (data) {

                var html = "<option></option>";

                $.each(data,function (index, ele) {

                    html+= "<option value='"+ele.id+"'>"+ele.title+"</option>";
                })

                //下拉框都使用相同的数据显示

                $("#strategy").html(html);


                //初始化2个组件(同时初始化两个下拉框 ,攻略下拉框,分类下拉框)调用样式要用.开头
                $(".chosen").chosen({

                    placeholder_text_single: "请选择攻略",
                    disable_search_threshold: 5,//大于10条就显示搜索框
                    no_results_text: "无相关攻略!"//搜索时找不到的提示
                });

                //攻略下拉框改变事件
                $("#strategy").change(function() {
                    //获取当前攻略下拉框的值
                    var strategyId = $(this).val();

                    //查询攻略分类集合
                    $.get('/strategyCatalog/listByStrategyId.do', {id: strategyId}, function (data) {
                        //设置攻略分类的下拉框
                        var html = "<option></option>";
                        $.each(data, function (index, ele) {
                            html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                        })

                        //下拉框都使用相同的数据显示

                        $("#strategyCatalog").html(html);
                        //让插件执行更新操作
                        $("#strategyCatalog").trigger("chosen:updated");

                    })
                })
            })
            //初始化ck(副文本)编辑器
            var ckeditor=CKEDITOR.replace('editor',{
                //设置编辑器高度,不需要带单位
                height:300
            });

        })
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
<#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">

                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label for="keyword">关键字:</label>
                    <input type="text" class="form-control" id="keyword" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输攻略标题">
                </div>

                <button id="btn_query" class="btn btn-default">查询</button>
                <button type="button" class="btn btn-success   addBtn">新增</button>
            </form>

            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                </tr>
                </thead>
            <#list result.list as entity>
                <tr>
                    <td>${entity_index+1}</td>
                    <td>${entity.title}</td>
                    <td><img src="${entity.coverUrl}" width="30px"/></td>
                    <td>${((entity.releaseTime)?string("yyyy-MM-dd HH:mm:ss"))!}</td>
                    <td>${(entity.sequence)!}</td>
                    <td>${(entity.catalog.name)!}</td>
                    <td>
                        <#if entity.state==0>
                            草稿
                        <#elseif entity.state==1>
                            发布

                        </#if>
                    </td>
                    <td><a href="javascript:void(0);" class="editBtn" data-json='${entity.jsonString}'>修改</a></td>
                </tr>
            </#list>
            </table>
            <div style="text-align: center;">
                <ul id="pagination" class="pagination"></ul>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="strategyDetailModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增/修改</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" id="strategyDetailId" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="title" name="title">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="title" class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverImg" width="50$"/>
                            <input type="hidden" class="form-control" id="coverUrl" name="coverUrl">
                            <input type="file" class="form-control" id="coverFile" name="file">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="title" class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control chosen" id="strategy">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="title" class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control chosen" id="strategyCatalog" name="catalog.id">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="title" class="col-sm-4 control-label">是否发布：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state" id="state">
                                <option value="0" selected>否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="title" class="col-sm-4 control-label">排序：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sequence" name="sequence">
                        </div>
                    </div>
                    <#--编辑模态框-->
                    <div class="form-group">
                        <textarea name="strategyContent.content" id="editor" rows="10" cols="80">

                        </textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="saveBtn">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


</body>
</html>